<?php
require_once 'environment.php';
if(!$acc->isLogged()) die(change_site("account.php"));

function findSunday(){
    //24 hours == 86400 seconds
    $time = time();
    $a_day = 86400;
    $days = 86400;
    $next_sun = $time + $days;
       while(date('N', $next_sun) != 7){
        $days += $a_day;
        $next_sun = $time+$days;
    }
    return strtotime(date('F j, Y 00:00', $next_sun));
}

function throwError($where, $what){
    $what = urlencode($what);
    print change_site("scripture.php?w={$where}&error={$what}");
    die();
}

$bible = new BibleVerseOfTheWeek($acc, $db);

$w = fixArr($_GET, "w");
switch($w){
    default:
        $temp->setTab(8);
        $temp->currentPage("Home >> Scripture");
        $temp->generate();
        print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />\n" : "")."</span>";
        print "<a href=\"scripture.php?w=add\">Add a weekly scripture</a><br />\n";
        $sql = $db->Execute("SELECT * FROM `monster`.`prayerOfWeek`");
        if($sql->RecordCount() > 0){
            while($row = $sql->FetchRow()){
                $expiration = date('F jS, Y', $row['Date']);
                if(time() > $row['Date']){
                    $expiration = "<span style=\"color: red;\">{$expiration}</span>";
                }else{
                    $expiration = "<span style=\"color: green;\">{$expiration}</span>";
                }
                print "<br />\n<b>Verse</b>: {$row['Verse']}<br />\n";
                print "<b>Text</b>: {$row['Text']}<br />\n";
                print "<b>Expires</b>: ".$expiration."<br />\n";
                print "<a href=\"scripture.php?w=delete&id={$row['AutoInc']}\">Delete</a>";
                if(time() < $row['Date']) print " | <a href=\"scripture.php?w=change&id={$row['AutoInc']}\">Change</a>";
                print "<br />\n";
            }
        }else{
            print "<br />\nThere seems to be no scriptures in the database.";
        }
        break;
    case("add"):
        $temp->setTab(8);
        $temp->currentPage("Home >> Scripture >> Add Weekly Verse");
        $temp->generate();
        if(!fixArr($_POST, "sunday")){
            print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />\n" : "")."</span>";
            print "<form method=\"POST\" action=\"\">\n";
            print "<label>Verse: <input type=\"text\" name=\"verse\" /></label><br />\n";
            print "<label>Expires: <select name=\"sunday\">";
            $nextSun = findSunday();
            for($i = $nextSun; date('Y', $i) < date('Y')+2; $i += 604800){ //604800 seconds in a week
                print "<option>".date('l F jS, Y', $i)."</option>\n";
            }
            print "</select></label><br />\n";
            print "<label>Text:<br />\n<textarea name=\"text\" style=\"resize: none;\" rows=\"3\" cols=\"40\"></textarea></label><br />\n";
            print "<input type=\"submit\" value=\"Add\" /> <input type=\"reset\" value=\"Reset\" />";
            print "\n</form>";
        }else{
            $verse = fixArr($_POST, "verse");
            $expires = fixArr($_POST, "sunday");
            $text = fixArr($_POST, "text");
            $return = $bible->addBibleVerse($verse, $text, $expires);
            if(is_array($return)){
                throwError("add", $return[1]);
            }else{
                print "Your verse has been added successfully!";
            }
        }
        break;
    case("change"):
        $temp->setTab(8);
        $temp->currentPage("Home >> Scripture >> Change Weekly Verse");
        $temp->generate();
        $id = fixArr($_GET, "id");
        if(!$id) throwError("default", "The ID you selected does not exist.");
        if(!($info = $bible->getBibleVerseByID($id))) throwError("default", "The ID you selected does not exist.");
        if(!fixArr($_POST, "sunday")){
            print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />\n" : "")."</span>";
            print "<form method=\"POST\" action=\"\">\n";
            print "<label>Verse: <input type=\"text\" name=\"verse\" value=\"{$info['Verse']}\" /></label><br />\n";
            print "<label>Expires: <select name=\"sunday\">";
            $nextSun = findSunday();
            for($i = $nextSun; date('Y', $i) < date('Y')+2; $i += 604800){ //604800 seconds in a week
                print "<option".(date('l F jS, Y', $i) == date('l F jS, Y', $info['Date']) ? " selected=\"selected\"" : "").">".date('l F jS, Y', $i)."</option>\n";
            }
            print "</select></label><br />\n";
            print "<label>Text:<br />\n<textarea name=\"text\" style=\"resize: none;\" rows=\"3\" cols=\"40\">{$info['Text']}</textarea></label><br />\n";
            print "<input type=\"submit\" value=\"Modify\" /> <input type=\"reset\" value=\"Reset\" />";
            print "\n</form>";
        }else{
            $verse = fixArr($_POST, "verse");
            $expires = fixArr($_POST, "sunday");
            $text = fixArr($_POST, "text");
            $return = $bible->editBibleVerse($id, $verse, $text, $expires);
            if(is_array($return)){
                throwError("change&id={$id}", $return[1]);
            }else{
                print "Your verse has been edited successfully!";
            }
        }
        break;
    case("delete"):
        $temp->setTab(8);
        $temp->currentPage("Home >> Scripture >> Delete Weekly Verse");
        $temp->generate();
        $id = fixArr($_GET, "id");
        if(!$id) throwError("default", "The ID you selected does not exist.");
        if(!($info = $bible->getBibleVerseByID($id))) throwError("default", "The ID you selected does not exist.");
        if(fixArr($_GET, "conf") !== "true"){
            print "Are you SURE you want to delete this verse ({$info['Verse']})? This can NOT be undone!<br />\n<br />\n";
            print "<a href=\"scripture.php\">No, don't delete it.</a><br />\n<br />\n";
            print "<a href=\"scripture.php?w=delete&id={$id}&conf=true\">Yes, delete it.</a>";
        }else{
            $return = $bible->removeBibleVerse($id);
            if(is_array($return)){
                throwError("default", $return[1]);
            }else{
                print "The verse was successfully deleted.";
            }
        }
        break;
}
?>